package chapter04;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * 字符串是不可变数据，大量字符串操作非常耗时，如果要频繁修改字符串
 * 建议使用stringbuilder或stringbuffer
 * StringBuilder效率高于StringBuffer
 * StringBuilder线程不安全,StringBUffer线程安全
 * Created by a on 2017/6/9.
 */
public class Test080 {
    public static void main(String[] args) {
        String appendStr = "";
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long startTime = System.currentTimeMillis();
        for (int i = 20000; i < 50000; i++) {
            appendStr+=(char)i;
        }
        long endTime = System.currentTimeMillis();
        System.out.println("String追加字符3万个.");
        System.out.println("用时:"+(endTime-startTime)/1000000d+"毫秒");

        StringBuilder stringBuilder = new StringBuilder();
        startTime =System.currentTimeMillis();
        for (int i = 20000; i < 500000; i++) {
            stringBuilder.append((char) i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuilder追加字符3万个。");
        System.out.println("用时:"+(endTime-startTime)/1000000d+"毫秒");

        StringBuffer stringBuffer = new StringBuffer();
        startTime = System.currentTimeMillis();
        for (int i = 20000; i < 50000; i++) {
            stringBuffer.append((char)i);
        }
        endTime = System.currentTimeMillis();
        System.out.println("StringBuffer追加字符3万个");
        System.out.println("用时:"+(endTime-startTime)/1000000d+"毫秒");

    }
}
